import {onUnmounted, ref, unref} from 'vue';


export interface PortalToFollowAction {

  setOpen:(open:boolean)=>void,
  toggleOpen:()=>void
}

export function usePortalToFollow() {

   const actionRef = ref<PortalToFollowAction|null>(null);

   onUnmounted(()=> {
     actionRef.value = null;
   })

   function register(action:PortalToFollowAction) {
      actionRef.value = action;
   }

   const methods:PortalToFollowAction = {
     setOpen:(open:boolean)=> {
        if(actionRef.value) {
            actionRef.value.setOpen(open)
        }
     },
     toggleOpen:()=> {
        if(actionRef.value) {
            actionRef.value.toggleOpen()
        }
     }
   }
   return [register,methods]
}